﻿Imports System.Data.OleDb
Public Class detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        img_like.Visible = False
        Dim sql As String = "SELECT * FROM Likes WHERE BuildName = @f1 AND LikedBy = @f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()

        cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)

        Dim odr As OleDbDataReader = cmd.ExecuteReader()

        If odr.HasRows Then
            img_like.Visible = True
            btn_like.Visible = False
        Else
            img_like.Visible = False
            btn_like.Visible = True
        End If

        conn.Close()

    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click

        Dim sql As String = "INSERT INTO Comments (userName_fkid,Comment,DateAdded,Build_fkid) VALUES (@f1,@f2,@f3,@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)




        cmd.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f2", tb_comment.Text)
        cmd.Parameters.AddWithValue("@f3", DateTime.Now.Date)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("ID"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("details.aspx?ID=" & Request.QueryString("ID"))

    End Sub

    Protected Sub btn_tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_tag.Click

        Dim sql As String = "INSERT INTO BuildTags (Buildfkid,Tagfkid) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        conn.Open()
        For Each cbl As ListItem In cbl_tags.Items
            If cbl.Selected = True Then
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
                cmd.Parameters.AddWithValue("@f2", cbl.Value)
                cmd.ExecuteNonQuery()
            End If
        Next






    End Sub

    Protected Sub btn_newtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_newtag.Click

        Dim sql As String = "INSERT INTO Tags (TagName) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_newtag.Text)
        cmd.ExecuteNonQuery()

        Dim cmdb As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID Desc", conn)
        Dim odr As OleDbDataReader = cmdb.ExecuteReader()
        odr.Read()
        Dim highestID = odr(0)

        Dim associate As String = "INSERT INTO BuildTags (Buildfkid,Tagfkid) VALUES (@f1,@f2)"
        Dim cmdc As OleDbCommand = New OleDbCommand(associate, conn)
        cmdc.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmdc.Parameters.AddWithValue("@f2", highestID)
        cmdc.ExecuteNonQuery()
    End Sub

    Protected Sub btn_rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rate.Click

        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        conn.Open()

        Dim delratings As String = "DELETE FROM Rating WHERE userName_fkid=@f1 AND BuildName=@f2"
        Dim delratingcmd As New OleDbCommand(delratings, conn)
        delratingcmd.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        delratingcmd.Parameters.AddWithValue("@f2", Request.QueryString("ID"))
        delratingcmd.ExecuteNonQuery()

        Dim sql As String = "INSERT INTO Rating (Rating,userName_fkid,BuildName) VALUES (@f1,@f2,@f3)"


        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f3", Request.QueryString("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()




    End Sub

    Protected Sub btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click

        img_like.Visible = True
        btn_like.Visible = False

        Dim sql As String = "INSERT INTO Likes (BuildName,LikedBy) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", Request.QueryString("ID"))
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()



    End Sub

End Class